package com.e0box.game.logicGraphics
{
	import com.e0box.game.logicGraphics.data.PlayerData;
	import com.e0box.game.logicGraphics.display.Canvas;
	import com.e0box.game.sound.SoundManager;
	import com.e0box.game.utils.Language;
	
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;
	import starling.utils.AssetManager;
	
	public class Game extends Sprite
	{
		public static var instance:Game;
		
		public var assets:AssetManager;
		
		private var _pause:Button;
		
		private var _isPause:Boolean;
		private var _timer:Timer;
		
		private var _isDead:Boolean = true;
		
		private var _mask:Quad;
		private var _loading:Image;
		
		public function Game()
		{
			super();
			
			instance = this;
			
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function onAdded(evt:Event):void
		{
			
			setTexture();
			setMask();
			setLoading();
			setSound();
			setInput();
			
			setMain();
			
//			showMask(true);
		}
		
		private function setTexture():void
		{
			assets = new AssetManager();
			
			var text:TextureAtlas;
			
			if(Capabilities.language == Language.LANGUAGE_ZH_CN){
				text = new TextureAtlas(Texture.fromBitmap(new Materials.textureCn()), XML(new Materials.textureXmlCn()));	
			}else{
				text = new TextureAtlas(Texture.fromBitmap(new Materials.textureEn()), XML(new Materials.textureXmlEn()));
			}
			
			assets.addTextureAtlas("texture",text);
		}
		
		private function setMask():void
		{
			_mask = new Quad(Options.STAGE_WIDTH,Options.STAGE_HEIGHT,0);
			_mask.alpha = .75;
			_mask.touchable = true;
		}
		
		private function setLoading():void
		{
			
			_loading = new Image(assets.getTexture(Options.LOADING_ASSET));
			_loading.touchable = false;
			_loading.pivotX = _loading.width >> 1;
			_loading.pivotY = _loading.height >> 1;
			_loading.x = Options.STAGE_WIDTH >> 1;
			_loading.y = Options.STAGE_HEIGHT >> 1;
		}
		
		private function setSound():void{
			
			//SoundManager.instance.put(Options.SOUND_SURVIVE_BG, Materials.BGM_1p_lv6);
			
		}
		
		private function setInput():void{
			
		}
		
		private function setMain():void{
			
			var c:Canvas = new Canvas(1,1);
			addChild(c);
			
			
		}
		
		public function showMask(showLoading:Boolean = false):void{
			addChild(_mask);
			if(showLoading){
				addChild(_loading);
				_loading.addEventListener(Event.ENTER_FRAME, onLoadingEnterFrame);
			}
		}
		
		public function removeMask():void{
			removeChild(_mask);
			removeChild(_loading);
			
			_loading.removeEventListener(Event.ENTER_FRAME, onLoadingEnterFrame);
		}
		
		private function onLoadingEnterFrame(evt:Event):void{
			_loading.rotation += Math.PI / 10;
		}
		
		public function pause(showClose:Boolean = true):void{
			_isPause = true;
			_pause.enabled = false;
			
			PlayerData.instance.save();
		}
		
		public function resume():void{
			_isPause = false;
			_pause.enabled = true;
			
		}
		
		public function mute():void{
			PlayerData.instance.mute = true;
			SoundManager.instance.mute();
			
		}
		public function loud():void{
			PlayerData.instance.mute = false;
			SoundManager.instance.loud();
		}
		
		public function get isPause():Boolean{
			return _isPause;
		}
		
		
		/*public static function getTexture(name:String):Texture
		{
			return instance.assets.getTexture(name);
		}*/
		
	}
}